package com.firefighting.appController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.firefighting.entity.Menu;
import com.firefighting.entity.Role;
import com.firefighting.entity.User;
import com.firefighting.service.MenuService;
import com.firefighting.service.RoleService;
import com.firefighting.service.UserService;
import com.firefighting.util.StringUtil;

@Controller
@RequestMapping("app")
public class LoginAppController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private RoleService roleService;
	static Logger logger = Logger.getLogger(LoginAppController.class);
	@RequestMapping("loginApp")
	public void loginApp(HttpServletRequest request,HttpServletResponse response) throws Exception{
		try {
			HttpSession session = request.getSession();
			String userName=request.getParameter("userName");
			String password=request.getParameter("password");
		//	String imageCode=request.getParameter("imageCode");

			//request.setAttribute("imageCode", imageCode);
			if(StringUtil.isEmpty(userName)||StringUtil.isEmpty(password)){
				request.setAttribute("error", "账户或密码为空");
				request.getRequestDispatcher("login.jsp").forward(request, response);
				return ;
			}
			User user = new User();
			user.setUsername(userName);
			user.setPassword(password);
			User currentUser = userService.loginUser(user);
			if(currentUser==null){
				request.setAttribute("error", "用户名或密码错误");
				request.getRequestDispatcher("login.jsp").forward(request, response);
			}else{
				
				// 登录信息存入session
				Role role = roleService.findOneRole(currentUser.getRoleid());
				String roleName = role.getRolename();
				currentUser.setRoleName(roleName);
              System.out.println("成功'");
						
				// 跳转到主界面
				response.sendRedirect("main.htm");
			}
		} catch (Exception e) {
			logger.error("用户登录错误",e);
			throw e;
		}
	
	}
	// 进入系统主界面
//		@RequestMapping("main")
//		public String toMain(HttpServletRequest request,HttpServletResponse response) throws Exception{
//			User currentUser = (User) request.getSession().getAttribute("currentUser");
//			if(currentUser == null ){
//				return null;
//			}
//						return "main";
//		}
		// 进入系统主界面
//		@RequestMapping("index")
//		public String toIndex(HttpServletRequest request,HttpServletResponse response){
//			return "index";
//		}
		
	//安全退出
		@RequestMapping("logout")
		public void logout(HttpServletRequest request,HttpServletResponse response) throws Exception{
			
				
			// 清空session
			request.getSession().invalidate();
			
			// 清空cookie
			Cookie[] cookies = request.getCookies();
			for (int i = 0; i < cookies.length; i++) {
				Cookie cookie = new Cookie(cookies[i].getName(), null);
				cookie.setMaxAge(0);
				response.addCookie(cookie);
			}

			response.sendRedirect("login.jsp");
		}
}

